﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

/* Gareth Williams
 * 
 * holds the model data for a cube definition
 * 
 */

namespace VuzixSample.model.geometry
{
    class Cube : IGeometry
    {
        private VertexBuffer vertices;
        private IndexBuffer indices;

        private const int number_of_vertices = 8;
        private const int number_of_indices = 36;

        public void InitialiseBuffers(float scale, GraphicsDeviceManager usingContext)
        {
            CreateCubeVertexBuffer(scale, usingContext);
            CreateCubeIndexBuffer(usingContext);
        }
        //rainbow cube - how virtual reality is that :)
        private void CreateCubeVertexBuffer(float scale, GraphicsDeviceManager graphics)
        {
            VertexPositionColor[] cubeVertices = new VertexPositionColor[number_of_vertices];

            cubeVertices[0].Position = Vector3.Multiply(new Vector3(-1, -1, -1), scale);
            cubeVertices[1].Position = Vector3.Multiply(new Vector3(-1, -1, 1), scale);
            cubeVertices[2].Position = Vector3.Multiply(new Vector3(1, -1, 1), scale);
            cubeVertices[3].Position = Vector3.Multiply(new Vector3(1, -1, -1), scale);
            cubeVertices[4].Position = Vector3.Multiply(new Vector3(-1, 1, -1), scale);
            cubeVertices[5].Position = Vector3.Multiply(new Vector3(-1, 1, 1), scale);
            cubeVertices[6].Position = Vector3.Multiply(new Vector3(1, 1, 1), scale);
            cubeVertices[7].Position = Vector3.Multiply(new Vector3(1, 1, -1), scale);

            cubeVertices[0].Color = Color.Black;
            cubeVertices[1].Color = Color.Red;
            cubeVertices[2].Color = Color.Yellow;
            cubeVertices[3].Color = Color.Green;
            cubeVertices[4].Color = Color.Blue;
            cubeVertices[5].Color = Color.Magenta;
            cubeVertices[6].Color = Color.White;
            cubeVertices[7].Color = Color.Cyan;

            vertices = new VertexBuffer(graphics.GraphicsDevice, VertexPositionColor.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
            vertices.SetData<VertexPositionColor>(cubeVertices);
        }
        private void CreateCubeIndexBuffer(GraphicsDeviceManager graphics)
        {
            UInt16[] cubeIndices = new UInt16[number_of_indices];

            //bottom face
            cubeIndices[0] = 0;
            cubeIndices[1] = 2;
            cubeIndices[2] = 3;
            cubeIndices[3] = 0;
            cubeIndices[4] = 1;
            cubeIndices[5] = 2;

            //top face
            cubeIndices[6] = 4;
            cubeIndices[7] = 6;
            cubeIndices[8] = 5;
            cubeIndices[9] = 4;
            cubeIndices[10] = 7;
            cubeIndices[11] = 6;

            //front face
            cubeIndices[12] = 5;
            cubeIndices[13] = 2;
            cubeIndices[14] = 1;
            cubeIndices[15] = 5;
            cubeIndices[16] = 6;
            cubeIndices[17] = 2;

            //back face
            cubeIndices[18] = 0;
            cubeIndices[19] = 7;
            cubeIndices[20] = 4;
            cubeIndices[21] = 0;
            cubeIndices[22] = 3;
            cubeIndices[23] = 7;

            //left face
            cubeIndices[24] = 0;
            cubeIndices[25] = 4;
            cubeIndices[26] = 1;
            cubeIndices[27] = 1;
            cubeIndices[28] = 4;
            cubeIndices[29] = 5;

            //right face
            cubeIndices[30] = 2;
            cubeIndices[31] = 6;
            cubeIndices[32] = 3;
            cubeIndices[33] = 3;
            cubeIndices[34] = 6;
            cubeIndices[35] = 7;

            indices = new IndexBuffer(graphics.GraphicsDevice, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
            indices.SetData<UInt16>(cubeIndices);

        }
        public void Render(GraphicsDevice usingContext)
        {
            usingContext.SetVertexBuffer(vertices);
            usingContext.Indices = indices;
            usingContext.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, number_of_vertices, 0, number_of_indices / 3);
        }
        public void Dispose()
        {
            indices.Dispose();
            vertices.Dispose();
        }
    }
}
